package com.lagou.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
     * 编程实现服务器类的初始化和关闭
     */
public class ServerInitClose {
    /**
     * 自定义成员方法实现服务器的初始化操作
     */
    private ServerSocket ss;
    private Socket s;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;

    public ObjectInputStream getOis() {
        return ois;
    }

    public void setSis(ObjectInputStream Ois) {
        this.ois = Ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    public void setOos(ObjectOutputStream oos) {
        this.oos = oos;
    }

    public void serverInit() throws IOException {
        //1、创建ServerSocket类型的对象并且提供端口号
        ss = new ServerSocket(9999);
        System.out.println("等待客户端的连接...");
        //2、等待客户端的连接，调用accept方法
        s = ss.accept();
        System.out.println("客户端连接成功！");
        //3、使用输入流和输出流进行通信
        ois = new ObjectInputStream(s.getInputStream());
        oos =new ObjectOutputStream(s.getOutputStream());
        System.out.println("服务器初始化成功！");

    }
    //服务器关闭
    public void serverClose() throws IOException {

        //4、关闭Socket并且释放有关资源

        oos.close();
        ois.close();
        s.close();
        ss.close();
        System.out.println("服务器关闭成功！");
    }

}

